package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.MaintainMalfunctionWork;
import org.springblade.modules.repair.service.IMaintainMalfunctionWorkService;
import org.springblade.modules.repair.service.approve.strategy.ApproveBusinessFlow;
import org.springblade.modules.repair.service.approve.strategy.ApproveBusinessStrategy;
import org.springblade.modules.repair.vo.BusinessApproveVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author ysq
 * @Date 2022/5/11
 **/
@RestController
@AllArgsConstructor
@RequestMapping("repair/business/flow/{type}")
@Api(value = "审批业务接口", tags = "审批业务接口")
public class FlowApproveController {

	private final IMaintainMalfunctionWorkService maintainMalfunctionWorkService;

	/**
	 * 分页
	 *
	 * @param param
	 * @param query
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页")
	public R<IPage<Map<String, Object>>> page(@PathVariable(value = "type") Integer type, @RequestParam Map<String, Object> param, Query query) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		return R.data(flowStrategy.page(param, query));
	}

	@PostMapping("/approve")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "执行审批")
	public R approve(@RequestBody BusinessApproveVO businessApprove, @PathVariable(value = "type") Integer type) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		return R.status(flowStrategy.approve(businessApprove));
	}

	@PostMapping("/reminder")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "催办")
	public R reminder(@RequestParam Long businessApproveId, @PathVariable(value = "type") Integer type) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		return R.status(flowStrategy.reminder(businessApproveId));
	}

	@PostMapping("/addApprove")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "添加审批流程")
	public R addApprove(@RequestBody BusinessApproveVO businessApprove, @PathVariable(value = "type") Integer type) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		return R.status(flowStrategy.addApprove(businessApprove));
	}

	@PostMapping("/cancel")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "撤回")
	public R cancel(@RequestParam Long businessApproveId, @PathVariable(value = "type") Integer type) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		return R.status(flowStrategy.cancel(businessApproveId));
	}

	@PostMapping("/close")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "关闭")
	public R close(@RequestParam Long businessApproveId, @PathVariable(value = "type") Integer type, Long planId, String closeDesc) {
		ApproveBusinessFlow flowStrategy = ApproveBusinessStrategy.get(type);
		//数量大于0表示维修工单
		List<MaintainMalfunctionWork> works = maintainMalfunctionWorkService.list(Wrappers.<MaintainMalfunctionWork>lambdaQuery().eq(MaintainMalfunctionWork::getPlanId, planId));
		if (works.size() > 0) {
			works.forEach(o -> {
				o.setCloseDesc(closeDesc);
				o.setCloseUserName(AuthUtil.getNickName());
			});
			maintainMalfunctionWorkService.updateBatchById(works);
		}

		return R.status(flowStrategy.close(businessApproveId));
	}

}
